import java.util.concurrent.CountDownLatch;

/**
 * @Author YuanYuan
 * @Date 2022/10/13
 * @Time 15:47
 */
public class countdownlatch {

    public static void main(String[] args) throws InterruptedException {
        //有5份文件需要下载
        CountDownLatch countDownLatch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {


            //创建5个线程来执行一批任务
            Thread thread = new Thread(()-> {
                System.out.println("开始执行任务" + Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("结束执行任务" + Thread.currentThread().getName());
                //记录完成
                countDownLatch.countDown();
            });
            thread.start();
        }

        //await进行阻塞等待,会等到所有的文件都下载完毕之后,才解除阻塞
        countDownLatch.await();
        countDownLatch.await
        System.out.println("文件下载完成!");
    }
}
